<template>
    <view class="bills-info">
        <view class="info-content">
            <view class="b-info">
                <!-- 订单详细数据组件 -->
                <orderinfo
                    :orderInfoData="orderInfos"
                    :customer_info="customer_info"
                    :address="address"
                    :setShippType="setShippType"
                    :isNotData="isNotData"
                    :isShowOrder="true"
                    :md="md"
                    @openConsult="openConsult"
                    @showCustmInfo="showCustmInfo"
                    @feeInfo="feeInfo"
                >
                </orderinfo>
                <view class="overflow-box" v-show="!isNotData">
                    <!-- 订单详细列表组件 -->
                    <orderlist
                        :orderInfo="orderInfos.goods_ls"
                        :md="md"
                        :orderInfos="orderInfos"
                        @updateFun="updatainfo"
                    ></orderlist>
                </view>
                <orderflexbottom
                    ref="loadBtnData"
                    :btnArr="btnArr"
                    @orderOpration="orderOpration"
                    v-show="!isNotData"
                ></orderflexbottom>
                <!-- 订单底部操作按钮组件 -->
                <empty class="empty-p-style" v-if="isNotData"></empty>
            </view>
            <operate ref="toggleItem" :md="md"></operate>
            <consult ref="consult" @orderOpration="orderOpration" title="协商记录"></consult>
            <popInputWin ref="popInputWin"></popInputWin>
            <!-- 收款，退款窗口 -->
            <payMent ref="payMent"></payMent>
            <!-- 备注详情 -->
            <noteMask
                ref="noteMask"
                :newNoteArr="newNoteArr"
                :orderInfos="orderInfos"
                :md="md"
                @updateFun="updatainfo"
            ></noteMask>
            <!-- 费用列表 -->
            <outlayList ref="outlayList"></outlayList>
            <!-- 打印选择 -->
            <popPrintWin ref="popPrintWin" @printType="printType"></popPrintWin>

            <!-- 二维码支付 -->
            <payQrcode ref="payQrcode"></payQrcode>

            <!-- 云单改价 -->
            <changePriceWin
                ref="changePriceWin"
                @changePrice="changePrice"
                type="YY"
            ></changePriceWin>

            <view class="c-info" v-show="showInfo">
                <customerInfo
                    :orderInfoData="orderInfos"
                    :address="address"
                    :newNoteArr="newNoteArr"
                    :customer_info="customer_info"
                    :md="md"
                    @openNoteMask="showNoteMask"
                    @showCustmInfo="showCustmInfo"
                ></customerInfo>
            </view>
            <selectShopList
                :shopList="shopList"
                ref="myShopList"
            ></selectShopList>
        </view>
    </view>
</template>

<script>
import orderinfo from "./OrderInfo.vue";
import orderlist from "./OrderList.vue";
import orderflexbottom from "./OrderFixedBottom.vue";
import customerInfo from "./customerInfo.vue";

import empty from "../comm/empty.vue";
import consult from "./ConsultMask.vue";

import config from "../../js/conf/Config.js";
import staticConst from "../../js/const/StaticConst.js";
import cmd from "../../js/Cmd.js";
import util from "../../js/util/Util.js";

import btnsContrl from "../../js/BtnsContrl.js";

import operate from "./operateMask.vue";
import popInputWin from "../comm/PopInputWin.vue";

import selectShopList from "../comm/SelectShopList.vue";

import outlayList from "../comm/OutlayList.vue";

import lang from "../../js/conf/Lang.js";

import payMent from "./PaymentMask.vue";

import noteMask from "./noteMask.vue";

import eventCtrl from "../../js/event/EventCtrl.js";

import https from "../../js/https/Https.js";

import printCtrl from "../../js/print/PrintCtrl.js";

import popPrintWin from "../comm/PopPrintWin.vue";

import payQrcode from "../comm/PayQrcode.vue";

import changePriceWin from "../comm/ChangePriceWin.vue";

let printSize = null;
let pageNo = 0;
export default {
    components: {
        orderinfo,
        orderlist,
        orderflexbottom,
        customerInfo,
        operate,
        selectShopList,
        popInputWin,
        payMent,
        noteMask,
        empty,
        outlayList,
        popPrintWin,
        payQrcode,
        changePriceWin,
        consult,
    },
    data() {
        return {
            title: "销售单详情",
            orderInfos: {},
            customer_info: {},
            address: {},
            bottomBtn: [],
            orderId: null,
            type: null,
            printSize: 241,
            showInfo: false,
            btnArr: [],
            operateLs: [],
            shopList: [],
            storeList: [],
            billListPageId: staticConst.BILLS_CLOUD,
            newNoteArr: [],
            setShippType: null, //运费
            isNotData: false, //没有数据
            isHasYunPrint: false, //是否有云打印
            md: staticConst.MD_DJ,
            expressOperation: "1",
            iseditLogistics: false,
            is_butloa: false,
            tableloading: false,
            kwvalue: "",
            carriageData: [],
            ogisticsInfolist: [],
            pfrom: 0,
            is_soll: false,
            isogisticsInfo: false,
        };
    },

    async created() {
        await this.getShopList();
        await this.getYunPrintList();
    },
    methods: {
        // 更新订单详情
        updatainfo(data) {
            const obj = {
                odid: data.id,
                type: data.type,
            };
            this.createdOrderInfo(obj);
        },
        /********打开费用详情********/
        feeInfo() {
            if (
                this.orderInfos &&
                this.orderInfos.fee_info &&
                this.orderInfos.fee_info.fee_ls
            ) {
                this.$refs.outlayList.show("show");
                let data = {};
                data.list = this.orderInfos.fee_info.fee_ls;
                data.fee_total = this.orderInfos.fee_total;
                data.amount_fee = this.orderInfos.amount_fee;
                this.$refs.outlayList.initData(data);
            }
        },

        openConsult() {
            let obj = {};
            obj.odid = this.orderInfos.id;
            this.$refs.consult.toggleBillsStateMask("show", obj);
            this.$refs.consult.ask_rt_status_act = this.orderInfos.ask_rt.status_act
        },

        /****获取是否有云打印列表******/
        getYunPrintList() {
            let that = this;
            let param = {};
            https.getPrintYunList(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    if (data.ls && data.ls.length > 0) {
                        that.isHasYunPrint = true;
                    }
                }
            }
        },

        /**获取门店**/
        async getShopList() {
            let url = config.SHOP_LS;
            let param = {};
            param.ismy = 1;
            let res = await this.$api({ url: url, data: param });
            if (!res) return;
            if (res.status == 1) {
                if (!res.data) return;
                this.shopList = res.data;
            }
        },

        /***  付款记录&操作记录弹窗  **/
        onOperate(type) {
            let obj = {};
            obj.type = this.orderInfos.type;
            obj.odid = this.orderInfos.id;
            obj.activeType = type;
            if (this.orderInfos.type == 4) {
                obj.del = 1;
                obj.odid = this.orderInfos.odid;
                obj.type = this.orderInfos.type_12;
            }
            this.$refs.toggleItem.orderOprAndPay(obj);
        },

        /*** 获取单据详情数据    YY0_ORDER_INFO ***/
        async createdOrderInfo(data) {
            // 解锁操作
            this.btnArr = [];
            if (this.type && this.orderId) {
                this.unlockOrder(this.type, this.orderId);
            }
            if (data != null) {
                let url = config.YY0_ORDER_INFO;
                let param = {};
                this.type = param.type = data.type;
                this.orderId = param.odid = data.odid;
                let res = await this.$api({ url: url, data: param });
                if (!res) return;
                if (res.status == 1 && res.data) {
                    this.setShipp(res.data);
                    this.orderInfos = res.data;
                    this.newNoteArr = res.data.txt_ls || [];
                    this.customer_info = res.data.customer_info;
                    this.address = res.data.address;
                    this.btnArr = btnsContrl.btnsYYOrderHandler(res.data);
                    console.log(this.btnArr,'btnArrbtnArrbtnArr')
                    if (printSize == null) {
                        this.getPrintSize(this.orderInfos.cpid);
                    }
                    if (res.data.lock_txt) {
                        this.$msg({ title: res.data.lock_txt });
                    }
                    this.isNotData = false;
                } else {
                    this.isNotData = true;
                }
            } else {
                this.isNotData = true;
            }
        },

        /*** 设置运费 ***/
        setShipp(order) {
            if (order.amount_shipp > 0) {
                this.setShippType = order.amount_shipp;
            } else {
                if (order.sfee_type == 1) {
                    this.setShippType = "包邮";
                } else if (order.sfee_type == 3) {
                    this.setShippType = "到付";
                } else {
                    this.setShippType = order.amount_shipp;
                }
            }
        },

        /*** 显示备注详情 ***/
        showNoteMask() {
            this.$refs.noteMask.toggleBillsStateMask("show");
        },

        /***获取企业打印模板尺寸***/
        async getPrintSize(cpid) {
            printSize = printCtrl.printSize;
            if (printSize == 0) {
                //==0重新请求
                printCtrl.getPrintSize(cpid, back);
            }
            function back(data) {
                printSize = data;
            }
        },

        /*** 解锁单号   ORDER_ACT ***/
        async unlockOrder(type, odid) {
            let url = config.YY0_ORDER_ACT;
            let param = {};
            param.type = type;
            param.odid = odid;
            param.act = staticConst.UNLOCK;
            let res = await this.$api({ url: url, data: param });
        },

        /*** 打印操作   PRINT_PAGE ***/
        print() {
            if (this.orderId && this.orderInfos.cpid) {
                this.localPrint(); //本地打印
            }
        },

        printType(type) {
            if (type == 0) {
                this.doYunPrint();
            } else {
                this.localPrint(); //本地打印
            }
        },

        /*******本地打印********/
        localPrint() {
            let data = {};
            data.cpid = this.orderInfos.cpid;
            data.orderId = this.orderId;
            data.type = staticConst.YY_ADD_GOODS;
            data.type_od = this.orderInfos.type;
            if (printSize == staticConst.PRINT_SIZE_241) {
                //241模板
                data.url = config.PRINT_URL_1;
            } else if (printSize == staticConst.PRINT_SIZE_80) {
                //80模板
                data.url = config.PRINT_URL_2;
            }

            printCtrl.print(data);
            this.printCount();
        },

        /***打印次数累计***/
        printCount() {
            let that = this;
            let url = config.PRINT_COUNT;
            let param = {};
            param.cpid = this.orderInfos.cpid;
            param.type = staticConst.YY_ADD_GOODS;
            param.odtype = this.orderInfos.type;
            param.odid = this.orderInfos.id;
            https.printCount(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    that.orderInfos.print++;
                }
            }
        },

        /****云打印*****/
        doYunPrint() {
            let that = this;
            let param = {};
            param.type = staticConst.YY_ADD_GOODS;
            param.type12 = this.orderInfos.type;
            param.cpid = this.orderInfos.cpid;
            param.odid = this.orderInfos.id;
            https.doYunPrint(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    if (!data.data) return;
                    console.log("doYunPrint().back():云打印成功！！！！");
                    that.$msg({ title: "提交云打印成功! 请稍等待..." });
                }
            }
        },

        /***关闭客户详情**/
        showCustmInfo() {
            this.showInfo = !this.showInfo;
            cmd.external(cmd.CMD_SHOW_MORE, this.showInfo);
        },

        /**单据操作***/
        orderOpration(act) {
            console.log("act============>", act);
            if (act === "tobook") {
                const data = {
                    orderInfos: this.orderInfos,
                };
                uni.$emit(eventCtrl.COPY_RETURN_TOBOOK, data);
            }
            if (act === "kdno_add") {
                this.iseditLogistics = true;
                this.expressOperation = "1";
                this.editedData = {};
            }
            if (act == "print") {
                if (this.isHasYunPrint) {
                    let data = {};
                    data.isLocal = false;
                    this.$refs.popPrintWin.show("show", data);
                } else {
                    this.print();
                }
            } else if (act == "del") {
                //删除
                let d = {};
                (d.content = "是否删除该单据？"), (d.showCancel = true);
                let obj = {};
                obj.act = act;
                obj.url = config.YY0_ORDER_ACT;
                d.data = obj;
                lang.showWin(d, callBack);
            } else if (act == "copy" || act == "return_ck" || act == "edt3") {
                //抄单,退货,编辑草稿
                this.coypReturnEditDirfet(act);
            } else if (act == "pay_in" || act == "pay_out") {
                //收款/退款
                this.toPay(act);
                //this.$refs.payMent.toggleBillsStateMask("show",{});
            } else if (act == "reject" || act == "edt") {
                //拒绝退货
                let obj = {};
                let d = [];
                obj.url = config.YY_ORDER_RETURN_CK;
                obj.act = act == "edt" ? act : "tock";
                obj.id_ask = this.orderInfos.ask_rt.id;
                if (act == "reject") obj.status = 1;
                d.data = obj;
                d.title = "拒绝理由";
                d.callBack = ydReturnCallBack;
                this.$refs.popInputWin.toggleBillsStateMask("show", d);
            } else if (act == "agree") {
                //同意退货
                // let d = {};
                // (d.content = "确认同意退货吗？"), (d.showCancel = true);
                let obj = {};
                obj.act = "tock";
                obj.status = 2;
                obj.url = config.YY_ORDER_RETURN_CK;
                // d.data = obj;
                this.$confirm('确认同意退货吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ydReturnCallBack(obj)
                }).catch(() => {});
                // lang.showWin(d, ydReturnCallBack);
            } else if (act == "op_recoder") {
                //操作记录
                this.onOperate(1);
            } else if (act == "pay_recoder") {
                //付款记录
                this.onOperate(0);
            } else if (act == "return_record") {
                //退款记录
                this.onOperate(2);
            } else if (act == "scrap") {
                //作废
                let d = {};
                (d.content = "是否作废该单据？"), (d.showCancel = true);
                let obj = {};
                obj.act = act;
                obj.url = config.YY0_ORDER_ACT;
                if (this.billListPageId == staticConst.BILLS_CLOUD) {
                    //云单作废要选择门店
                    obj.data = this.shopList;
                    if (this.shopList.length > 1) {
                        obj.titleType = 0;
                        obj.callBack = callBack;
                        this.$refs.myShopList.toggleAboutStateMask("show", obj);
                    } else {
                        if (this.shopList.length == 1) {
                            obj.id = this.shopList[0].id;
                            d.data = obj;
                            lang.showWin(d, callBack);
                        }
                    }
                } else {
                    d.data = obj;
                    lang.showWin(d, callBack);
                }
            } else if (act == "store_out" || act == "store_in") {
                //出库
                let obj = {};
                obj.url = config.YY0_ORDER_ACT;
                obj.data = this.storeList;
                obj.titleType = 1;
                obj.act = act;
                obj.callBack = callBack;
                this.$refs.myShopList.toggleAboutStateMask("show", obj);
            } else if (act == "tock") {
                //确认云单
                let obj = {};
                obj.url = config.YY0_ORDER_ACT;
                obj.data = this.shopList;
                obj.titleType = 0;
                obj.act = act;
                obj.callBack = callBack;
                this.$refs.myShopList.toggleAboutStateMask("show", obj);
            } else if (act == "custom_add") {
                this.$emit("addNewCustom"); //这里有问题，应要绑定一个订单客户
            } else if (act == "chprice") {
                let obj = {};
                obj.amount_shipp = this.orderInfos.amount_shipp;
                obj.amount_zero = this.orderInfos.amount_zero;
                obj.amount_all = this.orderInfos.amount_all;
                obj.amount_od = this.orderInfos.amount_od;

                this.$refs.changePriceWin.show("show", obj);
            }

            let that = this;
            async function callBack(data) {
                if (
                    data.cancel &&
                    data.data &&
                    (data.data.act == "scrap" || data.data.act == "del")
                )
                    return; //点击取消作废
                let url = data.url;
                if (!url)
                    //兼容
                    url = data.data.url;
                let param = {};
                param.type = that.orderInfos.type;
                param.odid = that.orderInfos.id;
                param.act = data.act;
                if (!param.act)
                    //兼容
                    param.act = data.data.act;
                if (param.act == "tock" || param.act == "scrap") {
                    //确认云单
                    param.shopid = data.id;
                    if (!param.shopid)
                        //兼容
                        param.shopid = data.data.id;
                }

                if (param.act == "store_out" || param.act == "store_in")
                    //出库
                    param.storeid = data.id;
                let res = await that.$api({ url: url, data: param });
                if (!res) return;
                if (res.status == 1) {
                    that.$msg({ title: res.msg });
                    if (param.act == "store_out" || param.act == "store_in") {
                        that.storeOutHandler(param.act);
                        return;
                    }
                    that.type = null;
                    that.orderId = null;
                    uni.$emit(eventCtrl.ACTION_ORDER_EVENT, param.odid);
                }
            }

            /****云单退货申请****/
            async function ydReturnCallBack(data) {
                // console.log(eventCtrl.ACTION_ORDER_EVENT,'ydReturnCallBackydReturnCallBackydReturnCallBack')
                let url = data.url;
                if (!url) url = data.data.url;
                let param = {};
                param.act = data.act;
                if (!param.act) param.act = data.data.act;

                param.id_ask = that.orderInfos.ask_rt.id;
                if (data.status) param.status = data.status;
                if (param.act == "tock" && !param.status)
                    param.status = data.data.status;
                if (data.txt) param.txt_cp = data.txt;
                let res = await that.$api({ url: url, data: param });
                if (!res) return;
                if (res.status == 1) {
                    that.type = null;
                    that.orderId = null;
                    that.$msg({ title: res.msg });
                    if (
                        (param.act == "tock" && param.status == 2) ||
                        param.act == "agree"
                    ) {
                        uni.$emit(
                            eventCtrl.ACTION_ORDER_EVENT,
                            that.orderInfos.id
                        );
                    }
                    if (act == "reject" || act == "edt") {
                        let data = {};
                        data.id = that.orderInfos.id;
                        data.type = 2;
                        uni.$emit(eventCtrl.PAY_IN_AND_OUT_EVENT, data);
                    }
                    let objdata = {
                        type: that.orderInfos.type,
                        odid: that.orderInfos.id,
                    };
                    that.createdOrderInfo(objdata);
                }
            }
        },

        /******修改云单价格********/
        async changePrice(data) {
            let param = {};
            let that = this;
            let url = config.YY0_ORDER_ACT;
            param.act = "chprice";
            param.price_new = {};
            param.price_new.amount_zero = data.amount_zero||0;
            param.price_new.amount_shipp = data.amount_shipp;
            param.type = this.orderInfos.type;
            param.odid = this.orderInfos.id;
            let res = await this.$api({ url: url, data: param });
            if (!res) return;
            if (res.status == 1) {
                let obj = {};
                obj.type = param.type;
                obj.odid = param.odid;
                await this.createdOrderInfo(obj);
                this.$emit("changeYunBillPrice", this.orderInfos);
            }
        },

        /***出库处理***/
        storeOutHandler(act) {
            this.btnArr.splice(this.btnArr.length - 1, 1);
            this.orderInfos.status_act.status_send_txt =
                act == "store_out" ? "已出库" : "已入库";
        },

        /****topay（去收款/去退款）**/
        async toPay(act) {
            let that = this;
            let param = {};
            let url = config.YY0_ORDER_PAY;
            param.type = this.orderInfos.type;
            param.odid = this.orderInfos.id;
            param.act = "topay";
            let res = await this.$api({ url: url, data: param });
            if (!res) return;
            if (res.status == 1) {
                if (!res.data) return;
                let obj = {};
                obj = res.data;
                obj.act = act;
                obj.odid = this.orderInfos.id;
                obj.type = this.orderInfos.type;
                obj.callBack = topayAction;
                this.$refs.payMent.toggleBillsStateMask("show", obj);
            }

            async function topayAction(data) {
                if (!data.payid || data.payid >= 1000) {
                    let param = data;
                    let url = config.YY0_ORDER_PAY;
                    let res = await that.$api({ url: url, data: param });
                    if (!res) return;
                    if (res.status == 1) {
                        that.$msg({ title: res.msg });
                        let obj = {};
                        let uData = {};
                        uData.id = obj.odid = that.orderInfos.id;
                        obj.type = that.orderInfos.type;
                        await that.createdOrderInfo(obj);
                        uData.status_pay_txt =
                            that.orderInfos.status_act.status_pay_txt;
                        uData.amount_notpay = that.orderInfos.amount_notpay;
                        uData.type = 1;
                        uni.$emit(eventCtrl.PAY_IN_AND_OUT_EVENT, uData);
                    }
                } else {
                    let obj = {};
                    obj.payType = data.paytype;
                    obj.objtype = staticConst.PAY_OBJ_02; //data.objtype;
                    obj.odid = data.odid;
                    obj.money_pay = data.amount_receive;
                    that.$refs.payQrcode.creatQrcode(obj);
                }
            }
        },

        /****抄单，退货，编辑草稿****/
        coypReturnEditDirfet(act) {
            let that = this;
            https.getYYCartInfo({}, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    data = data.data;
                    let has = false;
                    if (data.gd_ls) {
                        has = true;
                    }
                    that.cpReEdTip(act, has);
                }
            }
        },

        /*****抄单，退货，编辑草稿提示*****/
        cpReEdTip(act, has) {
            let msg = "";
            // if (has) msg = lang.commTipTxt;
            //1抄销售单、2抄退货单、3销售单退货、4草稿单编辑
            if (act == "copy") {
                msg = msg + lang.copyTipTxt;
            } else if (act == "return_ck") {
                msg = msg + lang.returnCkTipTxt;
            } else if (act == "edt3") {
                msg = msg + lang.editDirTipTxt;
            }
            let data = {};
            let that = this;
            data.content = msg;
            data.showCancel = true;
            lang.showWin(data, back);
            function back(res) {
                if (res.confirm) {
                    that.cpReEdAction(act);
                }
            }
        },

        /***请求抄单，退货，编辑草稿****/
        cpReEdAction(act) {
            let param = {};
            if (act == "copy") {
                if (this.orderInfos.type == 1) param.act = 1;
                else if (this.orderInfos.type == 2) param.act = 2;
            } else if (act == "return_ck") {
                param.act = 3;
            } else if (act == "edt3") {
                param.act = 4;
            }
            param.odid = this.orderInfos.id;
            https.copyYYReturnEdit(param, back);
            function back(data) {
                if (!data) return;
                if (data.status == 1) {
                    const data = {
                        act: param.act,
                        code: staticConst.YY_YY,
                    };
                    uni.$emit(eventCtrl.COPY_RETURN_EDIT_EVENT, data);
                }
            }
        },

        setBillListPageId(data) {
            this.removeEvent();
            this.billListPageId = data;
            this.addEvent();
        },

        /***注册事件****/
        addEvent() {
            uni.$on(eventCtrl.ORDER_LIST_EVENT, this.setBillListPageId);
        },

        removeEvent() {
            uni.$off(eventCtrl.ORDER_LIST_EVENT);
        },
    },

    mounted() {
        this.addEvent();
    },
};
</script>
<style lang="scss">
.bills-info {
    flex: 1;
    margin-left: 345px;
    height: 100vh;
    .overflow-box {
        // margin-top: 40px;
        overflow-y: auto;
        height: calc(100vh - 293px);
        button {
            margin: 10px 0;
            background: #007aff;
            color: #fff;
            font-size: 20px;
        }
    }
    .overflow-box::-webkit-scrollbar {
        /*滚动条整体样式*/
        width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
        height: 0px;
    }
    .overflow-box::-webkit-scrollbar-thumb {
        /*滚动条里面小方块*/
        border-radius: 10px;
        -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
        background: #ccc; //rgba(0, 0, 0, 0.0);
        scrollbar-arrow-color: red;
    }
    .overflow-box::-webkit-scrollbar-track {
        /*滚动条里面轨道*/
        -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0);
        border-radius: 0;
        background: rgba(0, 0, 0, 0);
    }

    .info-content {
        display: flex;
        justify-content: flex-start;
        height: calc(100% - 60px);
    }

    .b-info {
        // padding-right: 310px;
        // margin-right: 310px;
        width: 100%;
        border-right: 1px solid #ececec;
        background: #fff;
        .empty-p-style {
            padding-top: 110px;
        }
    }

    .c-info {
        // width: 310px;
        // height: 100%;
    }
}
</style>